#!/bin/bash

# Determine project name based on current directory
PROJECT=$(basename $PWD)
#
# NOTE: Don't like this?  Then uncomment one of the following as appropriate
#
# Just set it explictly to whatever you like:
# PROJECT=libseaside
#
# Parse it from any Qt *.pro or *.pri files in CWD:
# PROJECT=$(grep -E "TARGET ?= ?" *.pr[io]|cut -d' ' -f3)

# Grab most recent tag from git
TAG=$(git describe --tags --abbrev=0)

# If arg1 is provided, use it as commit/tag/tree-ish id to start
# the archive creation from...
[ "${1}z" != "z" ] && {
	TAG=${1}
	echo "Creating release starting from: ${TAG}"
}

VERSION=$(git describe --tags ${1})
VERSION=${VERSION//version-/}
VERSION=${VERSION//-g/.}
VERSION=${VERSION//-/+git}

# Set name of toplevel directory for the archive
BASE_PREFIX="${PROJECT}-${VERSION}/"

# Set name of resulting release archive file
BASE_ARCHIVE=${PROJECT}-${VERSION}.tar.gz

# Clean up any existing base package for this version
[ -e ${BASE_ARCHIVE} ] && rm -rf ${BASE_ARCHIVE} &&
	echo "Removed: ${BASE_ARCHIVE}"

# Generate the base package release tar ball
# NOTE: Becuase I used a git attribute that ignores the meego theme dir
#       this archive will not include it... that's intentional!
git archive --prefix=${BASE_PREFIX} HEAD  | gzip -c -- > ${BASE_ARCHIVE} && {
	echo "Created: ${BASE_ARCHIVE}"
} || {
	echo "Creation of release archive ${BASE_ARCHIVE} failed.  Reason unknown."
}
